﻿using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Globalization;
using System.Threading.Tasks;
using Xfm.TagHelpers.Enums;
using Xfm.TagHelpers.Exts;

namespace Xfm.TagHelpers
{
    /// <summary>
    /// 生成a或者button，如果有href属性就生成a标签
    /// </summary>
    public class BtTagHelper : TagHelper
    {
        //IXfTagOptions _options;
        public BtTagHelper()
        {
            //if (options == null)
            //{
            //    throw new ArgumentNullException(nameof(options));
            //}
            //_options = options;
            Flat = true;// options.Flat;
        }
        /// <summary>
        /// 背景色默认Default,其它Primary,Secondary,Info,Success,Warning,Danger
        /// </summary>
        public BtColor Color { get; set; } = BtColor.Default;
        /// <summary>
        /// 默认button,其他类型submit,reset
        /// </summary>
        public BtType Type { get; set; } = BtType.Button;
        /// <summary>
        /// 尺寸
        /// </summary>
        public UISize Size { get; set; } = UISize.Normal;
        /// <summary>
        /// 默认无圆角,圆角false
        /// </summary>
        public bool Flat { get; set; }
        /// <summary>
        /// 禁用状态
        /// </summary>
        public bool Disabled { get; set; } = false;
        /// <summary>
        /// btn-block,效果display: block;width: 100%;
        /// </summary>
        public bool Block { get; set; } = false;
        /// <summary>
        /// 空心按钮 btn-outline-背景色
        /// </summary>
        public bool Outline { get; set; } = false;
        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (context.AllAttributes.ContainsName("href"))
            {
                output.TagName = "a";
            }
            else
            {
                output.TagName = "button";
                output.Attributes.SetAttribute("type", Type.ToString().ToLower(CultureInfo.CurrentCulture));
            }
            output.AddClass("btn");
            if (Outline)
            {
                output.AddClass($"btn-outline-{Color.ToString().ToLower(CultureInfo.CurrentCulture)}");
            }
            else
            {
                output.AddClass($"btn-{Color.ToString().ToLower(CultureInfo.CurrentCulture)}");
            }
            if (Size != UISize.Normal)
                output.AddClass($"btn-{Size.ToString().ToLower(CultureInfo.CurrentCulture)}");
            if (Flat)
                output.AddClass("btn-flat");
            if (Disabled)
                output.AddClass("disabled");
            if (Block)
                output.AddClass("btn-block");
            return Task.CompletedTask;
        }
    }
}
